package com.buddies.lottery.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.components.support.RxFragment;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Description: Fragment 基类
 * Date: 2018/2/11
 * Author: xuliang
 */
public abstract class BaseFragment<P extends IPresenter> extends RxFragment implements IView {

    protected final String TAG = getClass().getSimpleName();

    protected BaseActivity mActivity;
    protected P mPresenter;
    protected View mView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (BaseActivity) getActivity();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }

    public BaseFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.mView = view;
        initView();
        initData();
    }

    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();

    public final <T extends View> T findViewById(@IdRes int id) {
        return mView.findViewById(id);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
        mView = null;
    }

    @Override
    public void onError(Throwable e) {
        if (mActivity != null) {
            mActivity.onError(e);
        }
    }

    public <T> void addSubscription(Observable<T> observable, DisposableObserver<T> observer) {
        observable.compose(this.<T>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer);
    }

    public <T> void addComputationSubscription(Observable<T> observable, DisposableObserver<T> observer) {
        observable.compose(this.<T>bindToLifecycle())
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer);
    }

    protected void postDelayed(Runnable runnable) {
        postDelayed(runnable, 0);
    }

    protected void postDelayed(Runnable runnable, long delay) {
        if (mActivity != null) {
            mActivity.postDelayed(runnable, delay);
        }
    }

    protected void removeDelayed(Runnable runnable) {
        if (mActivity != null) {
            mActivity.removeDelayed(runnable);
        }
    }

    public void showProgressDialog(String msg) {
        if (mActivity != null) {
            mActivity.showProgressDialog(msg);
        }
    }

    public void hideProgressDialog() {
        if (mActivity != null) {
            mActivity.hideProgressDialog();
        }
    }

}
